/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package leastsquaresapproximation;

import java.util.ArrayList;

/**
 *
 * @author PavelsKletnojs
 */
public class VandermondeMatrix {
    private ArrayList<ArrayList<Double>> matrix;
    
    public ArrayList<ArrayList<Double>> getMatrix(){
	return matrix;
    }
    
    public void createVandermondeMatrix(ArrayList<Double> x, int degree){
	degree++;
	matrix = new ArrayList<ArrayList<Double>>();
	matrix.add(new ArrayList<Double>());
	for(int i = 0; i < degree; i++){
	    double cell = 0;
	    for(int j = 0; j< x.size(); j++){
		cell += Math.pow(x.get(j), i);
	    }
	    matrix.get(0).add(cell);
	}
	
	for(int i = 1; i < degree; i++){
	    matrix.add(new ArrayList<Double>());
	    //Add cells from previous row starting from second
	    for(int j = 1; j < degree; j++){
		matrix.get(i).add( matrix.get(i-1).get(j) );
	    }
	    
	    //Add new cell at the end of row
	    double cell = 0;
	    for(int j = 0; j< x.size(); j++){
		cell += Math.pow(x.get(j), degree-1+i);
	    }
	    matrix.get(i).add( cell );
	}
    }
    
    public void appendMatrixWithY(ArrayList<Double> x, ArrayList<Double> y, int degree){
	degree++;
	for(int i = 0; i < degree; i++){
	    double cell = 0;
	    for(int j = 0; j < x.size(); j++){
		cell += Math.pow(x.get(j),i) * y.get(j);
	    }
	    matrix.get(i).add(cell);
	}
    }
    
    public void printMatrix(){
	for(ArrayList<Double> row : matrix){
	    for(Double cell : row){
		System.out.print(cell + "\t");
	    }
	    System.out.println();
	}
    }
}
